package priv.mill.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import priv.mill.controller.util.RetenObj;
import priv.mill.dao.mapper.AnswerMapper2;
import priv.mill.dao.mapper.UserInfoMapper;
import priv.mill.entry.Answer;
import priv.mill.entry.Question;
import priv.mill.entry.UserInfo;
import priv.mill.server.AnswerService2;
import priv.mill.server.QuestionService2;
import priv.mill.server.UserInfoService;
import priv.mill.util.TimeUtil;

@Controller
@RequestMapping("Question2")
public class QuestionController2 {

	@Autowired
	private QuestionService2 questionService;
	
	@Autowired
	private AnswerService2 AnswerService;
	@Autowired
	private UserInfoService userInfoService;
	private static final Logger logger = Logger.getLogger(QuestionController2.class);

	@RequestMapping("getQuestionById")
	public String getQuestionById(@RequestParam("QuestionId") Integer questionId) {
		try {
			Question questionById = questionService.getQuestionById(questionId);
			return "question/question";
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "";
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getQuestionByEntry")
	public String getQuestionByEntry(Question question2,ModelMap map,@RequestParam("pageNumber")Integer pageNumber) {
		try {
			question2 = questionService.getQuestionByEntry(question2);
			UserInfo userInfoById = userInfoService.getUserInfoById(1);
			Answer Answer = new Answer();
			Answer.setQId(question2.getQId());
			Answer.setPageNumber(pageNumber);
			Answer.setPageSize(5);
			List<Answer> answersByPage = AnswerService.getAnswersByPage(Answer);
			if (answersByPage!=null) {
				List<UserInfo> lUserInfos=new ArrayList<UserInfo>();
				for (Answer answer2 : answersByPage) {
					UserInfo userInfoById2 = userInfoService.getUserInfoById(answer2.getUId());
					lUserInfos.add(userInfoById2);
				}
				map.addAttribute("answersByPage", answersByPage);
				map.addAttribute("lUserInfos", lUserInfos);
			}
			int answerCount = AnswerService.getAnswerCount(question2.getQId());
			map.addAttribute("answerCount", answerCount);
			map.addAttribute("question2", question2);
			map.addAttribute("userInfoById", userInfoById);
			return "question/question";
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "error";
		}
	}
	
	@RequestMapping("getQid")
	@ResponseBody
	public RetenObj getQid(@RequestParam("qId")Integer qId,ModelMap map) {
		try {System.out.println(qId);
//			map.addAttribute("qId", qId);
//			return "question/question";
		return warpData(qId, 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
//			return "error";
		}
	}
	
	@RequestMapping("getQuestionsLikeTitle")
	public String getQuestionsLike(Question question,ModelMap map) {
		try {
			String title = new String(question.getQuesTitle().getBytes("iso8859-1"),"UTF-8");
			question.setQuesTitle(title);
			List<Question> questionsList = questionService.getQuestionsLike(question);
			if (questionsList!=null) {
				List<UserInfo> UserInfos=new ArrayList<UserInfo>();
				for (Question question2 : questionsList) {
					UserInfo userInfoById2 = userInfoService.getUserInfoById(question2.getUId());
					UserInfos.add(userInfoById2);
				}
				map.addAttribute("questionsList", questionsList);
				map.addAttribute("UserInfos", UserInfos);
			}
			return "question/questionsLikeTitle";
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "error";
		}
	}
	
	@RequestMapping("getQuestionsLikeName")
	public String getQuestionsLikeName(Question question,UserInfo user,ModelMap map) {
		try {
			//通过前台传来的用户名模糊查询是否有这个用户
			String name = new String(user.getUName().getBytes("iso8859-1"),"UTF-8");
			user.setUName(name);
			List<UserInfo> info = userInfoService.getUserInfoLikeName(user);
			List<List<Question>> list=new ArrayList<List<Question>>();
			//如果用户存在，就通过uid查询出问题
			if (info!=null) {
			for (UserInfo userInfo : info) {
					question.setUId(userInfo.getUId());
					List<Question> questionsList = questionService.getQuestionsLike(question);
					if (questionsList!=null) {
						list.add(questionsList);
					}
				}
			map.addAttribute("questionsList", list);
			map.addAttribute("UserInfos", info);
			}
			return "question/questionsLikeName";
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "error";
		}
	}
	
	@RequestMapping("getQuestionsList")
	public String getQuestionsList(Question question,ModelMap map) {
		try {
			List<Question> questionsList = questionService.getQuestionsList();
			if (questionsList!=null) {
				List<UserInfo> UserInfos=new ArrayList<UserInfo>();
				for (Question question2 : questionsList) {
					UserInfo userInfoById2 = userInfoService.getUserInfoById(question2.getUId());
					UserInfos.add(userInfoById2);
				}
				map.addAttribute("questionsList", questionsList);
				map.addAttribute("UserInfos", UserInfos);
			}
			return "index";
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "error";
		}
	}

	@RequestMapping("getQuestionsByPage")
	@ResponseBody
	public RetenObj getQuestionsByPage(Question question) {
		try {
			return warpData(questionService.getQuestionsByPage(question), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateQuestion")
	@ResponseBody
	public RetenObj updateQuestion(Question question) {
		try {
			int result = questionService.updateQuestion(question);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	
	@RequestMapping("insertQuestion2")
	public String insertQuestion(Question question,RedirectAttributes attr) {
		try {
			//设置当前时间为创建问题的时间
			question.setQCreateTime(TimeUtil.getTime());
			//设置当前时间为更新问题的时间
			question.setQUpdateTime(TimeUtil.getTime());
			//设置用户ID
			question.setUId(1);
			//设置问题是否被封禁
			question.setQuestionLock(0);
			//获取用户信息
			//调用方法向数据库插入问题
			int result = questionService.insertQuestion(question);
//			attr.addAttribute("pageNumber", 1);
			attr.addAttribute("qId", question.getQId());
			return "redirect:getQid";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}

//	@RequestMapping("insertQuestion2")
//	@ResponseBody
//	public RetenObj insertQuestion(Question question) {
//		try {
//			//设置当前时间为创建问题的时间
//			question.setQCreateTime(TimeUtil.getTime());
//			//设置当前时间为更新问题的时间
//			question.setQUpdateTime(TimeUtil.getTime());
//			//设置用户ID
//			question.setUId(1);
//			//设置问题是否被封禁
//			question.setQuestionLock(0);
//			//获取用户信息
//			UserInfo userInfoById = userInfoMapper.getUserInfoById(1);
//			//调用方法向数据库插入问题
//			int result = questionService.insertQuestion(question);
//			HashMap<String, Object> hashMap = new HashMap<String, Object>();
//			hashMap.put("question", question);
//			hashMap.put("userInfo", userInfoById);
//			//若插入成功，则将question对象返回前台页面
//			if (result > 0)
//				return warpData(hashMap, 200);
//			else
//				return warpData(hashMap, 300);
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//			return warpData(null, 500);
//		}
//	}


	@RequestMapping("deleteQuestion")
	
	@ResponseBody
	public RetenObj deleteQuestion(Integer questionId) {
		try {
			int result = questionService.deleteQuestion(questionId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
